// by Xeno

private ["_unit", "_caller", "_chatfunc", "_height", "_speed", "_s", "_hasbox", "_boxpos","_unitType","_marker_name"];

if (!X_Client) exitWith {};

_unit = _this select 0;
_caller = _this select 1;

if (_unit == _caller) then {_unit = d_curvec_dialog};
_unitType = typeof _unit;

// If the vehicle is not one of those which can load boxes... we don't look further.
_ammo_vec = _unit getVariable "d_ammotype";
if (isnil "_ammo_vec") then { _ammo_vec = false; };
if (count d_check_ammo_load_vecs > 0) then {
	if (!(_unitType in d_check_ammo_load_vecs) && !_ammo_vec) exitWith {diag_log Format[localize "MSG_DOM_VECCANTDROPBOX", d_curvec_dialog];};
};

_chatfunc = {
	if (vehicle (_this select 1) == (_this select 0)) then {
		[_this select 0, _this select 2] call XfVehicleChat;
	} else {
		[_this select 1, _this select 2] call XfSideChat;
	};
};

if (_unit distance AMMOLOAD < 50) exitWith {[_unit, _caller,localize "MSG_DOM_BOXNEARAMMO"] call _chatfunc};

_height = _unit call XfGetHeight;
if (_height > 3) exitWith {[_unit,localize "MSG_DOM_BOXDROPTOOHIGH"] call XfVehicleChat};
_speed = speed _unit;
if (_speed > 3) exitWith {[_unit,localize "MSG_DOM_BOXDROPTOOFAST"] call XfVehicleChat};

if ((["ammo_boxes",0] call XNetGetJIP) >= d_MaxNumAmmoboxes) exitWith {
	_s = format [localize "MSG_DOM_BOXTOOMANY", d_MaxNumAmmoboxes];
	[_unit,_caller,_s] call _chatfunc;
	[_unit,_caller,localize "MSG_DOM_BOXPICKONE"] call _chatfunc;
};

_hasbox = _unit getVariable "d_ammobox";
if (isNil "_hasbox") then {_hasbox = false};
if (!_hasbox) exitWith {[_unit, _caller, localize "MSG_DOM_BOXNONE"] call _chatfunc};

_time_next = _unit getVariable "d_ammobox_next";
if (isNil "_time_next") then {_time_next = -1};
if (_time_next > time) exitWith {[_unit, _caller, format [localize "MSG_DOM_BOXWAIT",round (_time_next - time)]] call _chatfunc};

[_unit, _caller, localize "MSG_DOM_BOXDROPPING"] call _chatfunc;

_unit setVariable ["d_ammobox", false, true];
_time_next = time + d_drop_ammobox_time;
_unit setVariable ["d_ammobox_next", _time_next, true];

_boxpos = _unit modelToWorld [4,0,0];
_boxpos set [2, 0];

_marker_name=Format["vehbox%1%2%3",floor(_boxpos select 0),floor(_boxpos select 1),floor(_boxpos select 2)];

["d_m_box", [_boxpos, _unit, _marker_name, d_own_side,d_vehicle_drop_box]] call XNetCallEvent;

[_unit, _caller, localize "MSG_DOM_BOXDROPPED"] call _chatfunc;